package be.android.forap.crypto;

import java.util.Scanner;

public class PatternUtil {

	public static final int PGP_MESSAGE = 1;
	public static final int PGP_SIGNED_MESSAGE = 2;
	public static final int PGP_PUBLIC_KEY = 3;
	public static final int PGP_PRIVATE_KEY = 4;
	
	private static final String PGP_MESSAGE_START = 		"-----BEGIN PGP MESSAGE-----";
	private static final String PGP_MESSAGE_END = 			"-----END PGP MESSAGE-----";
	
	private static final String PGP_SIGNED_MESSAGE_START = 	"-----BEGIN PGP SIGNED MESSAGE-----";
	private static final String PGP_SIGNED_MESSAGE_MIDDLE = "-----BEGIN PGP SIGNATURE-----";
	private static final String PGP_SIGNED_MESSAGE_END = 	"-----END PGP SIGNATURE-----";
	
	private static final String PGP_PUBLIC_KEY_START = 		"-----BEGIN PGP PUBLIC KEY BLOCK-----";
	private static final String PGP_PUBLIC_KEY_END =	 	"-----BEGIN PGP PUBLIC KEY BLOCK-----";
	
	private static final String PGP_PRIVATE_KEY_START = 	"-----BEGIN PGP PRIVATE KEY BLOCK-----";
	private static final String PGP_PRIVATE_KEY_END = 		"-----END PGP PRIVATE KEY BLOCK-----";
	


	public static boolean matchPGPBlock(String text, int type){
		String start = null;
		//String middle = null;
		String end = null;
		switch(type){
		case PGP_MESSAGE:
			start = PGP_MESSAGE_START;
			end = PGP_MESSAGE_END;
			break;
		case PGP_SIGNED_MESSAGE:
			start = PGP_SIGNED_MESSAGE_START;
			//middle = PGP_SIGNED_MESSAGE_MIDDLE;
			end = PGP_SIGNED_MESSAGE_END;
			break;
		case PGP_PUBLIC_KEY:
			start = PGP_PUBLIC_KEY_START;
			end = PGP_PUBLIC_KEY_END;
			break;
		case PGP_PRIVATE_KEY:
			start = PGP_PRIVATE_KEY_START;
			end = PGP_PRIVATE_KEY_END;
			break;
		}
		Scanner scanner = new Scanner(text);
		boolean matchingstart = false;
		boolean matchingend = false;
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			// process the line
			matchingstart = matchingstart || line.matches(start);
			matchingend = matchingend  ||line.matches(end);
		}
		return matchingstart && matchingend;
	}



	public static boolean isPrivateKeyBlock(String text) {
		Scanner scanner = new Scanner(text);
		boolean matchingstart = false;
		boolean matchingend = false;
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			// process the line
			matchingstart = matchingstart || line.matches("-----BEGIN PGP PRIVATE KEY BLOCK-----");
			matchingend = matchingend  ||line.matches("-----END PGP PRIVATE KEY BLOCK-----");
		}
		return matchingstart && matchingend;
	}
	
	public static boolean isEncryptedFile(String path){
		return path.matches("([^\\s]+(\\.(?i)(enc))$)");
	}
	
	public static boolean isPublicKeyFile(String path){
		return path.matches("([^\\s]+(\\.(?i)(asc))$)");
	}

	/*public static String PGP_PRIVATE_KEY = ".*(-----BEGIN PGP PRIVATE KEY BLOCK-----).*(-----END PGP PRIVATE KEY BLOCK-----).*";

	public static Pattern PGP_MESSAGE =
	Pattern.compile(".*?(-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----).*",
                Pattern.DOTALL);

	public static Pattern PGP_SIGNED_MESSAGE =
	Pattern.compile(".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*",
                Pattern.DOTALL);

	public static Pattern PGP_PUBLIC_KEY =
	Pattern.compile(".*?(-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----).*",
                Pattern.DOTALL);*/

}
